<template>
  <div class="regWrapper">
    <reg-top></reg-top>
    <reg-main v-on:message="receiverRegMsg"></reg-main>
    <box class="login-bottom-wrapper">
      <x-button type="warn" @click="register">同意使用协议并注册</x-button>
    </box>
  </div>
</template>
<script>
  import {XButton, Box, Toast} from './../../components'
  import regTop from './../components/login/regTop.vue'
  import regMain from './../components/login/regMain.vue'
  import {showBigGift, selectAddressStatus, bigGifts, QRCodeId} from './../../vuex/getters.js'
  import {set_showBigGift, set_selectAddressStatus, set_bigGift} from './../../vuex/actions.js'
  var sign = require("./../../commons/js/sign.js")
  var Global = require("./../../commons/js/Global.js")
  var LocalStorageUtil = require('./../../commons/js/LocalStorageUtil.js')
  var MyRequest = require('./../../commons/js/MyRequest.js')
  export default {
    components: {
      regTop,
      regMain,
      Box,
      XButton
    },
    data() {
      return {
        phone: "",
        pwd: "",
        code: "",
        invite: "",
        birthday: "",
        city: LocalStorageUtil.getItem("city")
      }
    },
    vuex: {
      getters: {
        showBigGift,
        selectAddressStatus,
        bigGifts,
        QRCodeId
      },
      actions: {
        set_showBigGift,
        set_selectAddressStatus,
        set_bigGift
      }
    },
    created: function () {
      Global.closeLoading()
    },
    methods: {
      register () {

        var data = {
          action: "register2",
          mobile: this.phone,
          password: this.pwd,
          cityId: this.city.id,
          code: this.code,
          platform: Global.getPlatform(),
          appVersion: Global.getVersion()
        }
        if (this.birthday) {
          data.birthday = this.birthday
        }
        if (this.invite) {
          data.referee = this.invite
        } else {
          data.referee = ''
        }
        var mysign = sign.getSign(data);
        data.sign = mysign;

        if (!this.judgeRegData()) {
          return
        }
        if (this.QRCodeId != "no") {
          var QRData = {
            QRCodeId: this.QRCodeId,
            userPhone: this.phone
          }
          MyRequest.MyGet("QRCodeId", this, function (result, that) {
          }, QRData);
        }
        MyRequest.MyGetWithToast("Register", this, function (result, that) {
          if (result.success) {
            Global.showToast(result.msg, that)
            LocalStorageUtil.setItem("User", result.data)
            that.getBigGift(that)
          } else {
            Global.closeLoading()
            Global.showToast(result.msg, that)
          }
        }, data)
      },
      receiverRegMsg (text) {
        this.phone = text.phone
        this.pwd = text.pwd
        this.code = text.code
        this.invite = text.invite
        this.birthday = text.birthday
      },
      getBigGift (that) {
        var data = {
          mobile: LocalStorageUtil.getItem("User").mobile
        }
        MyRequest.MyGetWithToast("BigGift", that, function (result, self) {
          if (result.success) {
            var mobile = LocalStorageUtil.getItem("User").mobile
            mobile = mobile.substring(mobile.length - 4, mobile.length)
            var curGifts = []
            for (var i = 0; i < result.data.length; i++) {
              var item = result.data[i]
              //console.log(item.bettwenTime)
              //item.activeTime = (item.bettwenTime.split("~"))[1]
              item.lastMobile = mobile
              curGifts.push(item)
            }
            console.log(curGifts)
            self.set_showBigGift(1)
            self.set_bigGift(curGifts)
            console.log(self.bigGifts)
            Global.closeLoading();
            self.$router.replace({path: '/'})
          } else {
            self.set_showBigGift(0)
          }
        }, data)
      },
      judgeRegData () {
        if (!(/^1[34578]\d{9}$/.test(this.phone))) {
          Global.showToast('请检查手机号格式', this)
          return false
        }
        if (!this.code) {
          Global.showToast('请输入验证码', this)
          return false
        }
        if (!this.pwd) {
          Global.showToast('请输入密码', this)
          return false
        }
        return true
      }
    }
  }
</script>
<style>
  .regWrapper .login-bottom-wrapper {
    padding: 2rem 2rem 0 2rem
  }
</style>
